package com.bw.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bw.service.GmallService;
import com.sugar.SugarUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @Author: Shi san
 * @Date: 2025/7/4 16:34
 * @Description:
 **/
@RestController
public class GmallController {
    @Autowired
    GmallService gmallService;

    @RequestMapping("/sumTotal")
    public Map SumTotal(String nowDate){
        Double value = gmallService.sumTotal(nowDate);
        return SugarUtil.getFlipNumberData(value);
    }

    @RequestMapping("/total")
    public Object total(String newDate){
        List<Map> total = gmallService.total(newDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        jsonObject.put("data",total.get(0).get("sum(total_amount)"));
        return jsonObject;
    }

    @RequestMapping("/basetotal")
    public Object basetotal(String newDate){
        List<Map> list = gmallService.basetotal(newDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONObject data = new JSONObject();
        JSONArray mapArray = new JSONArray();
        for (Map map : list) {
            JSONObject mapObject = new JSONObject();
            mapObject.put("name",map.get("name"));
            mapObject.put("value",map.get("value"));
            mapArray.add(mapObject);
        }

        data.put("mapData",mapArray);
        jsonObject.put("data",data);

        return jsonObject;
    }

    @RequestMapping("/bingtotal")
    public Object bingtotal(String newDate){
        List<Map> list = gmallService.bingtotal(newDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");

        JSONArray jsonArray = new JSONArray();
        for (Map map : list) {
            JSONObject dataObject = new JSONObject();
            dataObject.put("name",map.get("name"));
            dataObject.put("value",map.get("zb"));
            jsonArray.add(dataObject);
        }
        jsonObject.put("data",jsonArray);
        return jsonObject;
    }

    @RequestMapping("/zhutotal")
    public Object zhutotal(String newDate){
        List<Map> list = gmallService.zhutotal(newDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");

        JSONObject dataObject = new JSONObject();

        JSONArray x = new JSONArray();
        for (Map map : list) {
            x.add(map.get("name"));
        }
        dataObject.put("categories",x);

        JSONArray y = new JSONArray();
        JSONObject yjo = new JSONObject();
        yjo.put("name","品牌");
        jsonObject.put("data",dataObject);

        JSONArray dataArray = new JSONArray();
        for (Map map : list) {
            dataArray.add(map.get("sum"));
        }
        yjo.put("data",dataArray);
        y.add(yjo);
        dataObject.put("series",y);

        jsonObject.put("data",dataObject);
        return jsonObject;
    }

    @RequestMapping("/luntotal")
    public Object luntotal(String newDate){
        List<Map> list = gmallService.luntotal(newDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");

        JSONObject dataObject = new JSONObject();
        JSONArray dataArray = new JSONArray();

        JSONObject j1 = new JSONObject();
        j1.put("name","新老用户标志");
        j1.put("id","subsys");
        dataArray.add(j1);

        JSONObject j2 = new JSONObject();
        j2.put("name","用户人数");
        j2.put("id","yonghu");
        dataArray.add(j2);

        JSONObject j3 = new JSONObject();
        j3.put("name","总访问次数");
        j3.put("id","zcishu");
        dataArray.add(j3);

        JSONObject j4 = new JSONObject();
        j4.put("name","跳出率");
        j4.put("id","tiaochu");
        dataArray.add(j4);

        JSONObject j5 = new JSONObject();
        j5.put("name","平均在线时长");
        j5.put("id","pjtime");
        dataArray.add(j5);

        JSONObject j6 = new JSONObject();
        j6.put("name","平均访问人数");
        j6.put("id","pjrs");
        dataArray.add(j6);

        JSONArray rowArray = new JSONArray();
        for (Map map : list) {
            JSONObject rowObject = new JSONObject();
            rowObject.put("subsys",map.get("isnew"));
            rowObject.put("yonghu",map.get("count1"));
            rowObject.put("zcishu",map.get("sum"));
            rowObject.put("tiaochu",map.get("tc"));
            rowObject.put("pjtime",map.get("time"));
            rowObject.put("pjrs",map.get("count2"));
            rowArray.add(rowObject);
        }
        dataObject.put("columns",dataArray);
        dataObject.put("rows",rowArray);
        jsonObject.put("data",dataObject);
        return jsonObject;
    }
}
